<?php
$g4_path = '../';
$lib =  "$g4_path/lib.php";
$menu = 7;
require_once ("$g4_path/common.php");
require_once ($lib);
require_once ('../lib/Template_.class.php');

require_once ('./mydesk.lib.php');
require_once('../head.ebvill.php');

$tpl = new Template_;
$tpl->template_dir = './tpl';
$tpl->compile_dir = './tpl/compile';

$year = isset($_GET['year'])? $_GET['year'] : date('Y');
$month = isset($_GET['month'])? $_GET['month'] : date('n');
$data = $_tplSetting = array();

$nowPage = isset($_GET['page']) ? $_GET['page'] : 1; 
$perPage = 10;

$_res = sql_fetch("SELECT count(co.no) as cnt FROM course_order co, course c WHERE c.year='$year' AND c.month='$month' AND co.course = c.no AND co.student='$studentno'");

$_tplSetting['totalnum'] = $_res['cnt']; 
$totalPage = ceil($_res['cnt']/$perPage);

if(( ($nowPage > $totalPage) && $nowPage > 1) || ($nowPage < 1)) {
    alert("페이지 번호가 잘못되었습니다.");
}
$_sqlLimitPage = ($nowPage-1)*$perPage; 
$_sqlOrder = " ORDER BY co.no DESC"; 
$_sqlLimit = " LIMIT $_sqlLimitPage, $perPage"; 
$sql = "SELECT co.no, co.course, c.name, c.teacher, co.updated, co.status, co.no, c.w_mon, c.w_tue, c.w_wed, c.w_thu, c.w_fri FROM course_order co, course c WHERE c.year='$year' AND c.month='$month' AND co.course = c.no AND co.student='$studentno'".$_sqlOrder.$_sqlLimit;
$statusArray = array('1'=>'입금대기', '2'=>'입금완료', '3'=>'신청취소');
$_res = sql_query($sql);
while($_ret = sql_fetch_array($_res)) {
        $_temp = sql_fetch("SELECT mb_name FROM g4_member WHERE `mb_no`='".$_ret['teacher']."'"); 
        $_ret['teachername'] = $_temp['mb_name']; 
        $_ret['status'] = $statusArray[$_ret['status']]; 
        $weekArray = array($_ret['w_mon'], $_ret['w_tue'], $_ret['w_wed'], $_ret['w_thu'], $_ret['w_fri']); 
        $weekgenArray = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday');

        $lastday = $firstday = NULL; 
        for($i=0;$i<5;$i++) {
            if($weekArray[$i] > 0) { 
                $first_date = mktime ( 0, 0, 0, $month , 1, $year); 
                $last_date = mktime ( 0, 0, 0, $month+1, 1, $year)-86400;
                $sLastday = (date("w",$last_date) == $i+1) ? $last_date : strtotime("last ".$weekgenArray[$i], $last_date); 
                if(($sLastday > $lastday) || !$lastday) {
                    $lastday = $sLastday; 
                }
                $sFirstday = (date("w",$first_date) == $i+1) ? $first_date : strtotime("this ".$weekgenArray[$i], $first_date); 
                if(($sFirstday < $firstday) || !$firstday) {
                    $firstday = $sFirstday; 
                }
            }
        }

        $_ret['lastday'] = date('Y-m-d', $lastday);
        $_ret['firstday'] = date('Y-m-d', $firstday);
        $_ret['updated'] = substr($_ret['updated'], 0, 10);
        

        $data[] = $_ret; 
}


$thisYear = date('Y');
for($i = 2008; $i <= $thisYear+1; $i++) {
        $years[$i]= array('y' => "{$i}년");
        if($i == $_GET['year']) {
            $years[$i]['s'] = 'selected="selected"';
        }
}

$monthSelected = array($_GET['month']=>'selected="selected"');


if(!$totalPage) {
    $_tplSetting['nodata'] = TRUE; 
}
$_tplSetting['paging'] = get_paging($perPage,$nowPage,$totalPage,"./myCourseOrders.php?page=",makelink(array('page')));
$tpl->assign('data', $data);
$tpl->assign('set', $_tplSetting);
$tpl->define('basic',"myCourseOrders.html");
$tpl->print_('basic');
